﻿//---------------------------------------------------------------------------

#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "log.h"
#include "common.h"

#define	LOG_DIR		"/storage/emulated/0/Android/data/log"
//---------------------------------------------------------------------------

static TLog Log;
//---------------------------------------------------------------------------

//日志基础类
TLog::TLog() : FInit(false), Year(2000), Month(1), Day(1)
{
}
//---------------------------------------------------------------------------

void TLog::Initialize()
{
	if (!FInit)
	{
		TTIMEX dtime = GetDateTime();
		Year = dtime.Year;
		Month = dtime.Month;
		Day = dtime.Day;
		if(access(LOG_DIR, F_OK) < 0)
		{	//不存在./log则创建
			mkdir(LOG_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
		}
		SetLogFile();
		FInit = true;
	}
}
//---------------------------------------------------------------------------

//加锁
void TLog::Lock()
{
    ALock.Lock();
}
//---------------------------------------------------------------------------

//解锁
void TLog::Unlock()
{
    ALock.Unlock();
}
//---------------------------------------------------------------------------

//设置日志文件
void TLog::SetLogFile()
{
    memset(FFile, 0, 128);
    sprintf(FFile, "%s/MDC%d%02d%02d.log", LOG_DIR, Year, Month, Day);
}
//---------------------------------------------------------------------------

//写日志内容
void TLog::Write(char *pBuffer, int iSize)
{
	Lock();
	FILE *fp = fopen(FFile, "r+");
	if (!fp) fp = fopen(FFile, "w+");
	if(fp)
	{
		fseek(fp, 0, 2);
		fwrite(pBuffer, iSize, 1, fp);
		fflush(fp);
		int fd = fileno(fp);
		if (fd != -1) fsync(fd);
		fclose(fp);
	}
	Unlock();
}
//---------------------------------------------------------------------------

//输出日志内容
void TLog::Write(const char *pzOut)
{
	char buf[512] = { 0 };				//临时缓存区
	TTIMEX dt = GetDateTime();
	if ((Year != dt.Year) || (Month != dt.Month) || (Day != dt.Day))
	{
		Year = dt.Year;
		Month = dt.Month;
		Day = dt.Day;
		SetLogFile();
	}

	snprintf(buf, 512, "[%02d:%02d:%02d]%s\r\n", dt.Hour, dt.Min, dt.Sec, pzOut);
	Write(buf, strlen(buf));
}
//---------------------------------------------------------------------------

void LogInit()
{
	Log.Initialize();
}
//---------------------------------------------------------------------------

void LogOut(const char *pzInfo)
{
	Log.Write(pzInfo);
}
//---------------------------------------------------------------------------

//Hdr: 0x0A, Tail: 0x0B, middle: ASCII code
void LogOut(const char *pzHdr, BYTE *pData, int uSize)
{
	int m = strlen(pzHdr);
	int n = m + (uSize - 2) / 2 * 3 + 8;
	TAutoMem M1(n);
	BYTE *pOut = M1.GetBuffer();
	if(!pOut) return;
	BYTE *p = pOut;
	memcpy(p, pzHdr, m);
	p += m;
	BYTE *q = pData;
	ByteToAsc2(*q++, (BYTE*)p);
	p += 2;
	*p++ = 0x20;

	m = (uSize - 2) / 2;
	for(int i=0; i<m; i++)
	{
		*p++ = *q++;
		*p++ = *q++;
		*p++ = 0x20;
	}

	ByteToAsc2(*q, (BYTE*)p);
	LogOut((char*)pOut);
}
//---------------------------------------------------------------------------
